package com.sinocare.project.maintenance.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.maintenance.service.SccMedicalReportService;
import com.sinocare.project.common.entity.SccMedicalReport;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.maintenance.vo.SccMedicalReportVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 检测项初始数据表(SccMedicalReport)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:52:29
 */
@RestController
@RequestMapping("sccMedicalReport")
@CrossOrigin
public class SccMedicalReportController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccMedicalReportService sccMedicalReportService;

    /**
     * 分页查询所有数据
     *
     * @param page             分页对象
     * @param limit             每页条数
     * @param searchParams 查询json字符串
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Integer page,Integer limit,String searchParams) {
        QueryWrapper<SccMedicalReport> wrapper = new QueryWrapper<>();
        System.out.println(searchParams);
        if(!StrUtil.hasBlank(searchParams)){
            SccMedicalReportVo object = JSON.parseObject(searchParams, SccMedicalReportVo.class);
            wrapper.eq("medical_type",object.getMedicalType());
        }
        PageHelper.startPage(page,limit);
        List<SccMedicalReport> list = this.sccMedicalReportService.list(wrapper);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setReference(list.get(i).getReferemceLow()+"-"+list.get(i).getReferemceHigh());
        }
        PageInfo<SccMedicalReport> info = new PageInfo<>(list);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS,this.sccMedicalReportService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sccMedicalReport 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccMedicalReport sccMedicalReport) {
        sccMedicalReport.setStatus(1);
        sccMedicalReport.setMedicalStatus(1);
        DateTime date = DateUtil.date();
        sccMedicalReport.setCreateTime(date);
        sccMedicalReport.setUpdataTime(date);
        boolean save = this.sccMedicalReportService.save(sccMedicalReport);
        return Result.ok(ResultCode.SUCCESS,save);
    }

    /**
     * 修改数据
     *
     * @param sccMedicalReport 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccMedicalReport sccMedicalReport) {
        System.out.println(sccMedicalReport);
        UpdateWrapper<SccMedicalReport> wrapper = new UpdateWrapper<>();
        wrapper.eq("repot_id",sccMedicalReport.getRepotId());
        wrapper.set(!StrUtil.hasBlank(sccMedicalReport.getMedicalCode()),"medical_code",sccMedicalReport.getMedicalCode());
        wrapper.set(!StrUtil.hasBlank(sccMedicalReport.getMedicalName()),"medical_name",sccMedicalReport.getMedicalName());
        wrapper.set(!StrUtil.hasBlank(sccMedicalReport.getMedicalMethod()),"medical_method",sccMedicalReport.getMedicalMethod());
        wrapper.set(!StrUtil.hasBlank(sccMedicalReport.getMedicalReferemce()),"medical_referemce",sccMedicalReport.getMedicalReferemce());
        wrapper.set(!StrUtil.hasBlank(sccMedicalReport.getMedicalUnits()),"medical_units",sccMedicalReport.getMedicalUnits());
        wrapper.set(!String.valueOf(sccMedicalReport.getReferemceLow()).equals(""),"referemce_low",sccMedicalReport.getReferemceLow());
        wrapper.set(!String.valueOf(sccMedicalReport.getReferemceHigh()).equals(""),"referemce_high",sccMedicalReport.getReferemceHigh());
        boolean update = this.sccMedicalReportService.update(wrapper);
        return Result.ok(ResultCode.SUCCESS,update);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS,this.sccMedicalReportService.removeByIds(idList));
    }
}

